package com.neuedu.javase.api;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-09-29 8:38
 * 描述 : 日期Date
 */
public class MyDate {
    
    public static void main(String[] args){
        //创建对象
        Date now = new Date();  //获取当前日期对象
        long time = 1664412134235L;
        Date now1 = new Date(time);  // Thu Sep 29 08:42:14 CST 2022
        Date date1 = new Date(2022-1900,9-1,29);
        Date date2 = new Date(2022-1900,9-1,29,8,42);
        Date date3 = new Date(2022-1900,9-1,29,8,42,35);


        System.out.println("now = " + now);
        System.out.println("now1 = " + now1);
        System.out.println("date1 = " + date1);
        System.out.println("date2 = " + date2);
        System.out.println("date3 = " + date3);
        // 1970-1-1 到现在的毫秒数
        /**
         * now.getTime() =  1664412232355
         * now1.getTime() = 1664412134235
         */
        System.out.println("now.getTime() = " + now.getTime());
        System.out.println("now1.getTime() = " + now1.getTime());

        //2022-09-29  08:49:05
        //2022/09/29  08:49:05

        //日期的格式化

        //1 日期对象 ----> 字符串
        Date formatDate = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String formatStr = df.format(formatDate);
        System.out.println("formatStr = " + formatStr);

        //2 日期对象 ----> 字符串
        Scanner sc = new Scanner(System.in);

        //输入的字符串
        boolean switchD = false;  //是否执行 格式化日期的开关
        while(switchD) {
            System.out.print("请输入一个日期（yyyy-MM-dd HH:mm:ss）");
            String inputTime = sc.nextLine();
            if("e".equals(inputTime)){
                break;
            }
            try {
                Date parseDate = df.parse(inputTime);
                System.out.println("parseDate = " + parseDate);
            } catch (ParseException e) {
                System.out.println("日期输入不正确");
                e.printStackTrace();
            }
        }

        System.out.println("==================================");
        System.out.println("=============Date API=============");
        System.out.println("==================================");


        Date d1 = new Date(2022-1900,8,1);
        Date d2 = new Date(2022-1900,9,1);
        Date d3 = new Date(2022-1900,9,1);

        System.out.println("d1.before(d2) = " + d1.before(d2));
        System.out.println("d2.after(d1) = " + d2.after(d1));

        System.out.println("d1.after(d2) = " + d1.after(d2));
        System.out.println("d2.before(d1) = " + d2.before(d1));

        System.out.println("d1.compareTo(d2) = " + d1.compareTo(d2));  // -1
        System.out.println("d2.compareTo(d1) = " + d2.compareTo(d1));  // 1
        System.out.println("d2.compareTo(d3) = " + d2.compareTo(d3));  // 0

        //获取日期中某一个字段
        int year = d1.getYear();
        int month = d1.getMonth();
        int date = d1.getDate();
        int day = d1.getDay(); //星期几
        int hours = d1.getHours();
        int minutes = d1.getMinutes();
        int seconds = d1.getSeconds();
//        d1.toInstant()
        System.out.println("year = " + year);
        System.out.println("month = " + month);
        System.out.println("date = " + date);
        System.out.println("day = " + day);
        System.out.println("hours = " + hours);
        System.out.println("minutes = " + minutes);
        System.out.println("seconds = " + seconds);




    }
    
    
    
}
